<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="失物名称" prop="lostName">
        <el-input
          v-model="queryParams.lostName"
          placeholder="请输入失物名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="丢失时间" prop="lostTime">
        <el-date-picker clearable
          v-model="queryParams.lostTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择丢失时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:lost:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:lost:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:lost:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:lost:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div class="lost-card-wrapper" v-loading="loading">
      <el-row v-if="lostList.length" :gutter="16" class="lost-card-grid">
        <el-col
          v-for="item in lostList"
          :key="item.lostId"
          :xs="24"
          :sm="12"
          :md="6"
          :lg="4"
          :xl="4"
        >
          <div
            class="lost-card"
            :class="{ 'is-selected': ids.includes(item.lostId) }"
            @click="toggleSelect(item.lostId)"
          >
            <div class="lost-card__selector" @click.stop="toggleSelect(item.lostId)">
              <span class="selector-dot" :class="{ 'is-checked': ids.includes(item.lostId) }">
                <el-icon v-if="ids.includes(item.lostId)">
                  <Check />
                </el-icon>
              </span>
            </div>
            <div class="lost-card__labels" @click.stop>
              <el-tag size="small" effect="dark">{{ item.lostType || "未分类" }}</el-tag>
            </div>
            <div class="lost-card__image">
              <image-preview :src="item.image" :width="'100%'" :height="110" />
            </div>
            <div class="lost-card__body">
              <h3 class="lost-card__name">{{ item.lostName || "未命名失物" }}</h3>
              <p class="lost-card__meta">
                <span class="meta-label">联系方式</span>
                <span>{{ item.contact || "未填写" }}</span>
              </p>
              <p class="lost-card__meta">
                <span class="meta-label">丢失时间</span>
                <span>{{ item.lostTime ? parseTime(item.lostTime, '{y}-{m}-{d}') : "未填写" }}</span>
              </p>
            </div>
            <div class="lost-card__footer" @click.stop>
              <el-button
                link
                type="primary"
                icon="Edit"
                @click="handleUpdate(item)"
                v-hasPermi="['manage:lost:edit']"
              >修改</el-button>
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(item)"
                v-hasPermi="['manage:lost:remove']"
              >删除</el-button>
            </div>
          </div>
        </el-col>
      </el-row>
      <el-empty v-else description="暂无数据" />
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改失物招领对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="lostRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="失物名称" prop="lostName">
          <el-input v-model="form.lostName" placeholder="请输入失物名称" />
        </el-form-item>
        <el-form-item label="照片" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="丢失时间" prop="lostTime">
          <el-date-picker clearable
            v-model="form.lostTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择丢失时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失物类别" prop="lostType">
          <el-input v-model="form.lostType" placeholder="请输入失物类别" />
        </el-form-item>
        <el-form-item label="联系方式" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系方式" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Lost">
import { listLost, getLost, delLost, addLost, updateLost } from "@/api/manage/lost"
import { Check } from "@element-plus/icons-vue"

const { proxy } = getCurrentInstance()

const lostList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    lostName: null,
    image: null,
    lostTime: null,
    lostType: null,
    contact: null,
  },
  rules: {
    lostName: [
      { required: true, message: "失物名称不能为空", trigger: "blur" }
    ],
    image: [
      { required: true, message: "照片不能为空", trigger: "blur" }
    ],
    lostTime: [
      { required: true, message: "丢失时间不能为空", trigger: "blur" }
    ],
    lostType: [
      { required: true, message: "失物类别不能为空", trigger: "change" }
    ],
    contact: [
      { required: true, message: "联系方式不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询失物招领列表 */
function getList() {
  loading.value = true
  listLost(queryParams.value).then(response => {
    lostList.value = response.rows
    total.value = response.total
    ids.value = []
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    lostId: null,
    lostName: null,
    image: null,
    lostTime: null,
    lostType: null,
    contact: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("lostRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

watch(ids, value => {
  single.value = value.length !== 1
  multiple.value = !value.length
}, { deep: true, immediate: true })

function toggleSelect(lostId) {
  if (lostId === undefined || lostId === null) {
    return
  }
  const index = ids.value.indexOf(lostId)
  if (index > -1) {
    ids.value.splice(index, 1)
  } else {
    ids.value.push(lostId)
  }
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加失物招领"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _lostId = row.lostId || ids.value
  getLost(_lostId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改失物招领"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["lostRef"].validate(valid => {
    if (valid) {
      if (form.value.lostId != null) {
        updateLost(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addLost(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _lostIds = row.lostId || ids.value
  proxy.$modal.confirm('是否确认删除失物招领编号为"' + _lostIds + '"的数据项？').then(function() {
    return delLost(_lostIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/lost/export', {
    ...queryParams.value
  }, `lost_${new Date().getTime()}.xlsx`)
}

getList()
</script>

<style scoped>
.lost-card-wrapper {
  margin-top: 16px;
}

.lost-card-grid {
  margin-bottom: 20px;
}

.lost-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 16px 14px 14px;
  border-radius: 10px;
  border: 1px solid transparent;
  background-color: #ffffff;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.05);
  transition: all 0.2s ease;
  cursor: pointer;
}

.lost-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 22px rgba(64, 158, 255, 0.16);
}

.lost-card.is-selected {
  border-color: var(--el-color-primary);
  box-shadow: 0 18px 32px rgba(64, 158, 255, 0.28);
}

.lost-card__labels {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 2px;
}

.lost-card__image {
  border-radius: 10px;
  overflow: hidden;
}

.lost-card__body {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.lost-card__name {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  color: #303133;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lost-card__meta {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #606266;
}

.meta-label {
  color: #909399;
  margin-right: 6px;
}

.lost-card__footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.lost-card__selector {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 2;
}

.selector-dot {
  display: inline-flex;
  justify-content: center;
  align-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid #dcdfe6;
  background-color: rgba(255, 255, 255, 0.95);
  color: var(--el-color-primary);
  transition: all 0.2s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.selector-dot:hover {
  border-color: var(--el-color-primary);
}

.selector-dot.is-checked {
  background: var(--el-color-primary);
  border-color: var(--el-color-primary);
  color: #fff;
  box-shadow: 0 6px 18px rgba(64, 158, 255, 0.38);
}
</style>
